﻿using System.Data.Common;
using System;
using System.Text.RegularExpressions;

namespace EventBusNet6;

public class GhostscirptService : IGhostscirptService
{
    private string OutPutDir = string.Empty;
    private string ExePath = string.Empty;
    private string NoticeUrl = string.Empty;
    public GhostscirptService(IOptions<GhostscirptOptions> ghostscirptOptions)
    {
        string basePath = AppContext.BaseDirectory;
        ExePath = Path.Combine(basePath, ghostscirptOptions.Value.ExePath ?? "");
        NoticeUrl = ghostscirptOptions.Value.NoticeUrl ?? "";
        OutPutDir = ghostscirptOptions.Value.OutPutDir ?? "";

        var path = "{yyyy}{MM}\\{dd}";
        if (string.IsNullOrWhiteSpace(OutPutDir))
        {
            var reg = new Regex(@"(\{.+?})");
            var match = reg.Matches(path);
            match.ToList().ForEach(a =>
            {
                var str = DateTime.Now.ToString(a.ToString().Substring(1, a.Length - 2)); // 每天一个目录
                path = path.Replace(a.ToString(), str);
            });
            OutPutDir = Path.Combine(basePath, path);
        }
    }

    public string ConvertPDF2JPEG(string inputPath, string guid)
    {
        StringBuilder stringBuilder = new StringBuilder();
        string outputPath = Path.Combine(OutPutDir, guid);
        if (!Directory.Exists(outputPath))
        {
            Directory.CreateDirectory(outputPath);
        }
        //-dNOPAUSE -sDEVICE=pngalpha -r200 -dFirstPage=" + 1+ " -dLastPage=" + 2 + " -o " + destImagePath + " " + path
        stringBuilder.Append($"-dNOPAUSE -sDEVICE=jpeg -r200 -o {outputPath}\\%d.jpeg {inputPath}");
        var cmd = new ExecuteCommand(stringBuilder.ToString(), outputPath, ExePath, NoticeUrl, guid);
        CommandService.GetInstance().Add(cmd);
        return outputPath;
    }

    public string ConvertPDF2PNG(string inputPath, string guid)
    {
        StringBuilder stringBuilder = new StringBuilder();
        string outputPath = Path.Combine(OutPutDir, guid);
        if (!Directory.Exists(outputPath))
        {
            Directory.CreateDirectory(outputPath);
        }
        //-dNOPAUSE -sDEVICE=pngalpha -r200 -dFirstPage=" + 1+ " -dLastPage=" + 2 + " -o " + destImagePath + " " + path
        stringBuilder.Append($"-dNOPAUSE -sDEVICE=pngalpha -r200 -o {outputPath}\\%d.png {inputPath}");
        var cmd = new ExecuteCommand(stringBuilder.ToString(), outputPath, ExePath, NoticeUrl, guid);
        CommandService.GetInstance().Add(cmd);
        return outputPath;
    }

    public string PdfCompression(string inputPath, string guid)
    {
        //压缩:
        //    gs -sDEVICE = pdfwrite -dPDFSETTINGS =/ebook -dNOPAUSE -dBATCH -dQUIET -sOutputFile = output.pdf input.pdf

        //    不同的压缩模式

        //    -dPDFSETTINGS =/ screen,压缩比最大，输出文件最小，质量最低
        //    -dPDFSETTINGS =/ ebook,压缩比稍小，输出文件稍大，质量稍高
        //    -dPDFSETTINGS =/ prepress,输出文件信息同Acrobat Distiller "Prepress Optimized"设置
        //    -dPDFSETTINGS =/ default,缺省的，即大多数情况使用的压缩方式
        //    gs -sDEVICE = pdfwrite -dCompatibilityLevel = 1.4 - dDownsampleColorImages = true - dColorImageResolution = 130 - dNOPAUSE - dBATCH - sOutputFile = output.pdf input.pdf

        //    - dColorImageResolution = 130可以设置图像DPI大小\

        StringBuilder stringBuilder = new StringBuilder();
        string outputPath = Path.Combine(OutPutDir, guid);
        if (!Directory.Exists(outputPath))
        {
            Directory.CreateDirectory(outputPath);
        }
        //stringBuilder.Append($"-sDEVICE=pdfwrite -dPDFSETTINGS =/ebook -dNOPAUSE -dBATCH -dQUIET -sOutputFile={outputPath}\\output.pdf {inputPath}");
        stringBuilder.Append($"-sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile={outputPath}\\compress.pdf {inputPath}");
        var cmd = new ExecuteCommand(stringBuilder.ToString(), outputPath, ExePath, NoticeUrl, guid);
        CommandService.GetInstance().Add(cmd);
        return outputPath;
    }
    public string PdfSplit(string inputPath, string guid, int splitFirstPage,int splitLastPage)
    {

        StringBuilder stringBuilder = new StringBuilder();
        string outputPath = Path.Combine(OutPutDir, guid);
        if (!Directory.Exists(outputPath))
        {
            Directory.CreateDirectory(outputPath);
        }
        //-dNOPAUSE -sDEVICE=pngalpha -r200 -dFirstPage=" + 1+ " -dLastPage=" + 2 + " -o " + destImagePath + " " + path
        stringBuilder.Append($"-sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dQUIET  -dFirstPage={splitFirstPage} -dLastPage={splitLastPage} -sOutputFile={outputPath}\\split.pdf {inputPath}");
        var cmd = new ExecuteCommand(stringBuilder.ToString(), outputPath, ExePath, NoticeUrl, guid);
        CommandService.GetInstance().Add(cmd);

        //stringBuilder.Clear();
        //++splitFirstPage;
        //stringBuilder.Append($"-sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dQUIET -sOutputFile ={outputPath}\\sub2.pdf -dFirstPage={splitFirstPage} -dLastPage={splitLastPage}  {inputPath}");
        //var cmd2 = new ExecuteCommand(stringBuilder.ToString(), outputPath, ExePath, NoticeUrl, guid);
        //CommandService.GetInstance().Add(cmd2);
        return outputPath;
    }

    public string PdfCombined(string inputPath, string guid)
    {
        //合并:
        //gs - sDEVICE = pdfwrite - dNOPAUSE - dBATCH - dQUIET - sOutputFile = combined.pdf sub1.pdf sub2.pdf

        StringBuilder stringBuilder = new StringBuilder();
        string outputPath = Path.Combine(OutPutDir, guid);
        if (!Directory.Exists(outputPath))
        {
            Directory.CreateDirectory(outputPath);
        }
        //-dNOPAUSE -sDEVICE=pngalpha -r200 -dFirstPage=" + 1+ " -dLastPage=" + 2 + " -o " + destImagePath + " " + path
        stringBuilder.Append($"-sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dQUIET -sOutputFile={outputPath}\\combined.pdf  {inputPath}");
        var cmd = new ExecuteCommand(stringBuilder.ToString(), outputPath, ExePath, NoticeUrl, guid);
        CommandService.GetInstance().Add(cmd);
        return outputPath;
    }

    public string Pdf2PngOptPage(string inputPath,string guid, int firstPage, int lastPage)
    {
        StringBuilder stringBuilder = new StringBuilder();
        string outputPath = Path.Combine(OutPutDir, guid);
        if (!Directory.Exists(outputPath))
        {
            Directory.CreateDirectory(outputPath);
        }
        //-dNOPAUSE -sDEVICE=pngalpha -r200 -dFirstPage=" + 1+ " -dLastPage=" + 2 + " -o " + destImagePath + " " + path
        stringBuilder.Append($"-dNOPAUSE -sDEVICE=pngalpha -r200 -dFirstPage={firstPage} -dLastPage={lastPage}  -o {outputPath}\\%d.png {inputPath}");
        var cmd = new ExecuteCommand(stringBuilder.ToString(), outputPath, ExePath, NoticeUrl, guid);
        CommandService.GetInstance().Add(cmd);
        return outputPath;
    }
}
